package com.bilibili.bilibili.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.bilibili.bilibili.R;
import com.bilibili.bilibili.adapter.CommendAdapter;
import com.bilibili.bilibili.model.RecommendModel;
import com.bilibili.bilibili.network.HttpGetData;
import com.bilibili.bilibili.network.VolleyInterface;
import com.bilibili.bilibili.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment {

    private int i=1;
    private TextView text_zonghe;
    private XRecyclerView recycler;
    private CommendAdapter adapter;
    private List<RecommendModel.DataBean> list=new ArrayList<>();
     private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int tag=msg.arg1;
            List<RecommendModel.DataBean> data= (List<RecommendModel.DataBean>) msg.obj;
            switch (tag){
                case 1:
                    list.clear();
                    list.addAll(data);
                    adapter.notifyDataSetChanged();
                    recycler.refreshComplete();
                    break;
                case 2:
                    list.addAll(data);
                    adapter.notifyDataSetChanged();
                    recycler.loadMoreComplete();
                    break;
                case 3:
                    list.addAll(data);
                    adapter = new CommendAdapter(getActivity(), list);
                    recycler.setLayoutManager(new GridLayoutManager(getActivity(),2));
                    recycler.setAdapter(adapter);
                    break;
            }
        }
    };
    private TextView textView;
    private RelativeLayout rl;
    private LinearLayout ll;


    public RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = View.inflate(getActivity(), R.layout.fragment_recommend, null);
        text_zonghe = (TextView) view.findViewById(R.id.recommend_text_zonghe);
        recycler = (XRecyclerView) view.findViewById(R.id.recommed_recycler);
    //    mViewstub = (ViewStub) view.findViewById(R.id.mViewstub);
        textView = (TextView) view.findViewById(R.id.text_tongtai);
        rl = (RelativeLayout) view.findViewById(R.id.fail_rl);
        ll = (LinearLayout) view.findViewById(R.id.commend_ll);
        getData2(3);//获取数据
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
      //  showNetError();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"点击了",Toast.LENGTH_SHORT).show();

            }
        });
      recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
          @Override
         public void onRefresh() {
            new Thread(new Runnable() {
            			@Override
            			public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            getData2(1);
                        }
            		}).start();
         }

         @Override
         public void onLoadMore() {
                    new Thread(new Runnable() {
                    			@Override
                    			public void run() {
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    getData2(2);
                                }
                    		}).start();
         }
     });

    }
    private void getData2(final int tag){
        String path=null;
        if(tag==1){
            path="http://app.bilibili.com/x/feed/index?appkey=1d8b6e7d45233436&build=500001&idx=1489057149&mobi_app=android&network=wifi&platform=android&pull=true&ts=1489058253000&sign=e680da1865dc3924721bdb07a5972f66&style="+(i+=2);
        }else if(tag==2){
            path="http://app.bilibili.com/x/feed/index?appkey=1d8b6e7d45233436&build=500001&idx=1489057149&mobi_app=android&network=wifi&platform=android&pull=true&ts=1489058253000&sign=e680da1865dc3924721bdb07a5972f66&style="+(i+=1);
        }else if(tag==3){
            path="http://app.bilibili.com/x/feed/index?appkey=1d8b6e7d45233436&build=500001&idx=1489057149&mobi_app=android&network=wifi&platform=android&pull=true&ts=1489058253000&sign=e680da1865dc3924721bdb07a5972f66&style="+i;
        }
        HttpGetData.RequestGet(getActivity(), path, "djn", new VolleyInterface(getActivity(),VolleyInterface.mListener,VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                Log.i("TAG", "onMySuccess: "+result);
                RecommendModel recommendModel = GsonUtil.parseJsonToObj(result,new TypeToken<RecommendModel>(){
                });
               /* Gson gson=new Gson();
                RecommendModel model = gson.fromJson(result, RecommendModel.class);*/
                List<RecommendModel.DataBean> data = recommendModel.getData();
                Message message=new Message();
                message.obj=data;
                message.arg1=tag;
                handler.sendMessage(message);
//                adapter.setMyitemclistener(new CommendAdtapter.Myitemclistener() {
//                    @Override
//                        Toast.makeText(getActivity(),"点击了"+position,Toast.LENGTH_LONG).show();
//                    }
//                });

            }
            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(getActivity(),"加载失败",Toast.LENGTH_SHORT).show();
             // showNetError();
             // mViewstub.setVisibility(View.VI  SIBLE);
                rl.setVisibility(View.VISIBLE);
                ll.setVisibility(View.GONE);
            }
        });

    }








}
